package com.cug.nageoffer.user.controller;

import com.cug.nageoffer.model.entity.Post;
import com.cug.nageoffer.model.vo.PostVo;
import com.cug.nageoffer.model.vo.Result;
import com.cug.nageoffer.model.vo.ResultCodeEnum;
import com.cug.nageoffer.user.service.PostService;
import com.cug.nageoffer.user.util.ElasticSearchUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.cug.nageoffer.common.constant.ElasticSearch.MAPPING_TEMPLATE;

@Slf4j
@Tag(name = "用户发帖相关")
@RestController
@RequestMapping("/api/seeker/user/post")
public class PostController {

    @Resource
    private PostService postService;

    @Resource
    private ElasticSearchUtil elasticSearchUtil;

    /**
     * 用户发帖
     */
    @Operation(summary = "用户发帖")
    @PostMapping("/post")
    public Result<String> post(@RequestBody Post requestParam) throws IOException {
        return postService.post(requestParam);
    }

    /**
     * 用户删除帖子
     */
    @Operation(summary = "用户删帖")
    @PostMapping("/delete")
    public Result<String> deletePost(@RequestParam Long id){
        return postService.delete(id);
    }

    /**
     * 查询所有帖子
     */
    @Operation(summary = "查询所有帖子")
    @GetMapping("/getAll")
    public Result<List<PostVo>> getAll(){
        return postService.getAll();
    }

    /**
     * 根据id查询帖子
     */
    @Operation(summary = "根据id查询帖子")
    @PostMapping("/getById")
    public Result<List<Post>> getById(@RequestParam List<Long> ids){
        return postService.getById(ids);
    }

    /**
     * 查询用户所发帖子
     */
    @Operation(summary = "查询用户所发帖子")
    @PostMapping("/getByUser")
    public Result<List<Post>> getByUser(@RequestParam List<Long> ids){
        return postService.getByUser(ids);
    }

    /**
     * es添加索引库
     */
    @Operation(summary = "es添加索引库")
    @PostMapping("/addIndex")
    public Result<String> addIndex() throws IOException {
        elasticSearchUtil.createIndex("post",MAPPING_TEMPLATE);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * es添加文档
     */
    @Operation(summary = "es添加文档")
    @PostMapping("/addDoc")
    public Result<String> addDoc(@RequestBody Post post) throws IOException {
        elasticSearchUtil.addDocument(post.getId().toString(),"post",post);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询文档
     */
    @Operation(summary = "查询文档")
    @PostMapping("/getDocById")
    public Result<Post> getDocById() throws IOException {
        return Result.build(elasticSearchUtil.getDocumentById(1,"post"),ResultCodeEnum.SUCCESS);
    }

    /**
     * 检索查询文档
     */
    @Operation(summary = "检索查询文档")
    @PostMapping("/searchDoc")
    public Result<List<Post>> searchDoc(@RequestParam String value) throws IOException {
        return Result.build(elasticSearchUtil.getDocumentByMatch("post","title",value),ResultCodeEnum.SUCCESS);
    }
}
